let increaseL = false //标识是增加左压力还是减少左压力
let increaseR = false //标识是增加右压力还是减少右压力
let increaseC = false //标识是增加中压力还是减少中压力

export default {
  namespaced: true,
  state: {
    // 双状态泵 (true/false)
    lEngPump: false, //左EDP
    rEngPump: false, //右EDP
    c1ElecPump: false, //中C1泵
    c2ElecPump: false, //中C2泵

    // 三状态泵 (off/on/auto)
    lElecPump: 'AUTO', //左电泵
    c1AirPump: 'AUTO', //中C1空气泵
    c2AirPump: 'AUTO', //中C2空气泵
    rElecPump: 'AUTO', //右电泵

    lPress: 0, //左压力
    rPress: 0, //右压力
    cPress: 0, //中压力

    lPressInterval: null,
    rPressInterval: null,
    cPressInterval: null,

    ratPump: false // 冲压空气涡轮泵
  },
  mutations: {
    // 双状态泵的 mutations
    setLEngPump: (state) => (state.lEngPump = !state.lEngPump),
    setREngPump: (state) => (state.rEngPump = !state.rEngPump),
    setC1ElecPump: (state) => (state.c1ElecPump = !state.c1ElecPump),
    setC2ElecPump: (state) => (state.c2ElecPump = !state.c2ElecPump),

    // 三状态泵的 mutations
    setLElecPump: (state, mode) => (state.lElecPump = mode),
    setC1AirPump: (state, mode) => (state.c1AirPump = mode),
    setC2AirPump: (state, mode) => (state.c2AirPump = mode),
    setRElecPump: (state, mode) => (state.rElecPump = mode),

    // Mutations for press values
    setLPress: (state, value) => {
      if (value > 3000) {
        value = 3000
      }
      if (value < 0) {
        value = 0
      }
      state.lPress = value
    },
    setRPress: (state, value) => {
      if (value > 3000) {
        value = 3000
      }
      if (value < 0) {
        value = 0
      }
      state.rPress = value
    },
    setCPress: (state, value) => {
      if (value > 3000) {
        value = 3000
      }
      if (value < 0) {
        value = 0
      }
      state.cPress = value
    },

    setRatPump: (state, value) => (state.ratPump = value)
  },
  actions: {
    // 双状态泵的 actions
    setLEngPump({ commit }) {
      commit('setLEngPump')
    },
    setREngPump({ commit }) {
      commit('setREngPump')
    },
    setC1ElecPump({ commit }) {
      commit('setC1ElecPump')
    },
    setC2ElecPump({ commit }) {
      commit('setC2ElecPump')
    },

    // 三状态泵的 actions
    setLElecPump({ commit }, mode) {
      commit('setLElecPump', mode)
    },
    setC1AirPump({ commit }, mode) {
      commit('setC1AirPump', mode)
    },
    setC2AirPump({ commit }, mode) {
      commit('setC2AirPump', mode)
    },
    setRElecPump({ commit }, mode) {
      commit('setRElecPump', mode)
    },

    // Actions to increase/decrease lPress
    increaseLPress({ state, commit }) {
      increaseL = true
      if (state.lPressInterval) {
        clearInterval(state.lPressInterval)
        state.lPressInterval = null
      }
      state.lPressInterval = setInterval(() => {
        if (state.lPress < 3000) {
          commit('setLPress', state.lPress + 11)
        } else {
          clearInterval(state.lPressInterval)
          state.lPressInterval = null
        }
      }, 100)
    },
    decreaseLPress({ state, commit }) {
      increaseL = false
      if (state.lPressInterval) {
        clearInterval(state.lPressInterval)
        state.lPressInterval = null
      }
      state.lPressInterval = setInterval(() => {
        if (state.lPress > 0) {
          commit('setLPress', state.lPress - 11)
        } else {
          clearInterval(state.lPressInterval)
          state.lPressInterval = null
        }
      }, 100)
    },

    // Actions to increase/decrease rPress
    increaseRPress({ state, commit }) {
      increaseR = true
      if (state.rPressInterval) {
        clearInterval(state.rPressInterval)
        state.rPressInterval = null
      }
      state.rPressInterval = setInterval(() => {
        if (state.rPress < 3000) {
          commit('setRPress', state.rPress + 11)
        } else {
          clearInterval(state.rPressInterval)
          state.rPressInterval = null
        }
      }, 100)
    },
    decreaseRPress({ state, commit }) {
      increaseR = false
      if (state.rPressInterval) {
        clearInterval(state.rPressInterval)
        state.rPressInterval = null
      }
      state.rPressInterval = setInterval(() => {
        if (state.rPress > 0) {
          commit('setRPress', state.rPress - 11)
        } else {
          clearInterval(state.rPressInterval)
          state.rPressInterval = null
        }
      }, 100)
    },

    // Actions to increase/decrease cPress
    increaseCPress({ state, commit }) {
      increaseC = true
      if (state.cPressInterval) {
        clearInterval(state.cPressInterval)
        state.cPressInterval = null
      }
      state.cPressInterval = setInterval(() => {
        if (state.cPress < 3000) {
          commit('setCPress', state.cPress + 11)
        } else {
          clearInterval(state.cPressInterval)
          state.cPressInterval = null
        }
      }, 100)
    },
    decreaseCPress({ state, commit }) {
      increaseC = false
      if (state.cPressInterval) {
        clearInterval(state.cPressInterval)
        state.cPressInterval = null
      }
      state.cPressInterval = setInterval(() => {
        if (state.cPress > 0) {
          commit('setCPress', state.cPress - 11)
        } else {
          clearInterval(state.cPressInterval)
          state.cPressInterval = null
        }
      }, 100)
    },

    // Method to stop lPress interval
    stopLPressInterval({ state }) {
      if (state.lPressInterval) {
        clearInterval(state.lPressInterval)
        state.lPressInterval = null
      }
    },

    // Method to stop rPress interval
    stopRPressInterval({ state }) {
      if (state.rPressInterval) {
        clearInterval(state.rPressInterval)
        state.rPressInterval = null
      }
    },

    // Method to stop cPress interval
    stopCPressInterval({ state }) {
      if (state.cPressInterval) {
        clearInterval(state.cPressInterval)
        state.cPressInterval = null
      }
    },

    // Method to restart lPress interval
    restartLPressInterval({ state, commit, dispatch }) {
      if (increaseL) {
        dispatch('increaseLPress')
      } else {
        dispatch('decreaseLPress')
      }
    },

    // Method to restart rPress interval
    restartRPressInterval({ state, commit, dispatch }) {
      if (increaseR) {
        dispatch('increaseRPress')
      } else {
        dispatch('decreaseRPress')
      }
    },

    // Method to restart cPress interval
    restartCPressInterval({ state, commit, dispatch }) {
      if (increaseC) {
        dispatch('increaseCPress')
      } else {
        dispatch('decreaseCPress')
      }
    },

    setRatPump({ state, commit }) {
      commit('setRatPump', !state.ratPump)
    }
  },
  getters: {
    // 双状态泵的 getters
    lEngPump: (state) => state.lEngPump,
    rEngPump: (state) => state.rEngPump,
    c1ElecPump: (state) => state.c1ElecPump,
    c2ElecPump: (state) => state.c2ElecPump,

    // 三状态泵的 getters
    lElecPump: (state) => state.lElecPump,
    c1AirPump: (state) => state.c1AirPump,
    c2AirPump: (state) => state.c2AirPump,
    rElecPump: (state) => state.rElecPump,

    // Getters for press values
    lPress: (state) => state.lPress,
    rPress: (state) => state.rPress,
    cPress: (state) => state.cPress,

    ratPump: (state) => state.ratPump
  }
}
